@using BlazorApp.Pages.Common
@using BlazorApp.Pages.Common.Html.HTable
@using BlazorApp.Pages.Common.Metadata
@using BlazorApp.Pages.Event
@using BlazorApp.Pages.Gateway.Share
@using BlazorApp.Pages.Service
@using BlazorApp.Utils
@using Entity.Crd.Gateway
@using k8s.Models
@using Mapster
@inherits BlazorApp.Pages.Common.DrawerPageBase<Entity.Crd.Gateway.V1GRPCRoute>
@if (GrpcRoute != null)
{
    <GrpcRouteAction Item="GrpcRoute" MenuMode="MenuMode.Horizontal"></GrpcRouteAction>
    <Divider Orientation="left" Style="font-weight:bold">@L["Metadata"]</Divider>
    <MetadataView Item="GrpcRoute.Metadata" ExplainFieldPrefix="grpcRoute"></MetadataView>
    <Divider Orientation="left" Style="font-weight:bold">@L["Spec"]</Divider>
    <HTable Bordered Column="1">
        <PropertyListView Title=@L["Hostnames"] Items="GrpcRoute.Spec.Hostnames" T="string"
                          ExplainField="grpcRoute.spec.hostnames">
        </PropertyListView>
    </HTable>

    <Divider Orientation="left" Style="font-weight:bold">@L["Parent"]</Divider>
    <RouteParentRefsView ParentRefs="GrpcRoute.Spec.ParentRefs"
                         ExplainFieldPrefix="grpcRoute.spec.parentRefs">
    </RouteParentRefsView>

    @if (GrpcRoute.Spec.Rules is { Count: > 0 })
    {
        <Divider Orientation="left" Style="font-weight:bold">@L["Rules"]</Divider>
        @foreach (var rule in GrpcRoute.Spec.Rules)
        {
            <HTable Bordered Column="1">
                <PropertySimpleView Title="@L["Matches"]" Item="@rule.Matches"
                                    ExplainField="grpcRoute.spec.rules.matches">
                    @foreach (var item in rule.Matches)
                    {
                        <HTable Title="" Bordered Column="1">

                            <PropertySimpleView Title="@L["Method"]" Item="@item.Method"
                                                ExplainField="grpcRoute.spec.rules.matches.method">
                                <ColorfulTag>@item.Method.Type</ColorfulTag>
                                <ColorfulTag>@item.Method.Service</ColorfulTag>
                                <ColorfulTag>@item.Method.Method</ColorfulTag>
                            </PropertySimpleView>
                            <PropertySimpleView Title="@L["Headers"]" Item="@item.Headers">
                                @foreach (var header in item.Headers)
                                {
                                    <GridRow Class="grid-table">
                                        <GridCol Span="8">@header.Type</GridCol>
                                        <GridCol Span="16">@($"{header.Name}={header.Value}")</GridCol>
                                    </GridRow>
                                }
                            </PropertySimpleView>
                        </HTable>
                    }
                </PropertySimpleView>
                <BackendRefsView BackendRefWithWeight=@(rule.BackendRefs.Adapt<List<BackendRefWithWeight>>())
                                 ExplainField="grpcRoute.spec.rules.backendRefs"
                                 Ns="@GrpcRoute.Metadata.Namespace()">
                </BackendRefsView>

                <PropertySimpleView Title="@L["Filters"]" Item="@rule.Filters"
                                    ExplainField="grpcRoute.spec.rules.filters">
                    @foreach (var item in rule.Filters)
                    {
                        <HTable Title="" Bordered Column="1">
                            <PropertySimpleView Title="@L["Type"]" Item="@item.Type"
                                                ExplainField="grpcRoute.spec.rules.filters.type">
                            </PropertySimpleView>
                            <PropertySimpleView Title="@L["ExtensionRef"]" Item="@item.ExtensionRef"
                                                ExplainField="grpcRoute.spec.rules.filters.extensionRef">

                                <GridRow Class="grid-table">
                                    <GridCol Span="6">Group</GridCol>
                                    <GridCol Span="6">Kind</GridCol>
                                    <GridCol Span="6">Name</GridCol>
                                    <GridCol Span="6">Ref</GridCol>
                                </GridRow>
                                <GridRow Class="grid-table">
                                    <GridCol Span="6">@item.ExtensionRef.Group</GridCol>
                                    <GridCol Span="6">@item.ExtensionRef.Kind</GridCol>
                                    <GridCol Span="6">@item.ExtensionRef.Name</GridCol>
                                    <GridCol Span="6">
                                        <RefView FullView="true"
                                                 Ref="@KubeHelper.GetObjectRef(item.ExtensionRef.Kind, GrpcRoute.Metadata.Namespace(), item.ExtensionRef.Name)">
                                        </RefView>
                                    </GridCol>
                                </GridRow>
                            </PropertySimpleView>
                            <PropertySimpleView Title="@L["RequestMirror"]" Item="@item.RequestMirror"
                                                ExplainField="grpcRoute.spec.rules.filters.requestMirror">
                                <HTable Title="" Bordered Column="1">
                                    <PropertySimpleView Title="@L["Ref"]" Item="@item.RequestMirror.BackendRef.Name">
                                        <RefView FullView="true"
                                                 Ref="@KubeHelper.GetObjectRef(item.RequestMirror.BackendRef.Kind, item.RequestMirror.BackendRef.Namespace, item.RequestMirror.BackendRef.Name)">
                                        </RefView>
                                    </PropertySimpleView>
                                    <PropertySimpleView Title="@L["Group"]" Item="@item.RequestMirror.BackendRef.Group"
                                                        ExplainField="grpcRoute.spec.rules.filters.requestMirror.backendRef.group">
                                    </PropertySimpleView>
                                    <PropertySimpleView Title="@L["Kind"]" Item="@item.RequestMirror.BackendRef.Kind"
                                                        ExplainField="grpcRoute.spec.rules.filters.requestMirror.backendRef.kind">
                                    </PropertySimpleView>
                                    <PropertySimpleView Title="@L["Namespace"]"
                                                        Item="@item.RequestMirror.BackendRef.Namespace"
                                                        ExplainField="grpcRoute.spec.rules.filters.requestMirror.backendRef.namespace">
                                    </PropertySimpleView>
                                    <PropertySimpleView Title="@L["Name"]" Item="@item.RequestMirror.BackendRef.Name"
                                                        ExplainField="grpcRoute.spec.rules.filters.requestMirror.backendRef.name">
                                    </PropertySimpleView>
                                    <PropertySimpleView Title="@L["Port"]" Item="@item.RequestMirror.BackendRef.Port"
                                                        ExplainField="grpcRoute.spec.rules.filters.requestMirror.backendRef.port">
                                    </PropertySimpleView>
                                </HTable>
                            </PropertySimpleView>

                            <PropertySimpleView Title="@L["RequestHeaderModifier"]" Item="@item.RequestHeaderModifier"
                                                ExplainField="grpcRoute.spec.rules.filters.requestHeaderModifier">
                                <GridRow Class="grid-table">
                                    <GridCol Span="6">Set</GridCol>
                                    <GridCol Span="9">Add</GridCol>
                                    <GridCol Span="9">Remove</GridCol>
                                </GridRow>
                                <GridRow Class="grid-table">
                                    <GridCol Span="6">
                                        @if (item.RequestHeaderModifier.Set is { Count: > 0 })
                                        {
                                            @foreach (var set in item.RequestHeaderModifier.Set)
                                            {
                                                <ColorfulTag>@($"{set.Name}={set.Value}")</ColorfulTag>
                                            }
                                        }
                                    </GridCol>
                                    <GridCol Span="9">
                                        @if (item.RequestHeaderModifier.Add is { Count: > 0 })
                                        {
                                            @foreach (var add in item.RequestHeaderModifier.Add)
                                            {
                                                <ColorfulTag>@($"{add.Name}={add.Value}")</ColorfulTag>
                                            }
                                        }
                                    </GridCol>
                                    <GridCol Span="9">
                                        @if (item.RequestHeaderModifier.Remove is { Count: > 0 })
                                        {
                                            @foreach (var remove in item.RequestHeaderModifier.Remove)
                                            {
                                                <ColorfulTag>@remove</ColorfulTag>
                                            }
                                        }
                                    </GridCol>
                                </GridRow>
                            </PropertySimpleView>
                            <PropertySimpleView Title="@L["ResponseHeaderModifier"]" Item="@item.ResponseHeaderModifier"
                                                ExplainField="grpcRoute.spec.rules.filters.responseHeaderModifier">
                                <GridRow Class="grid-table">
                                    <GridCol Span="6">Set</GridCol>
                                    <GridCol Span="9">Add</GridCol>
                                    <GridCol Span="9">Remove</GridCol>
                                </GridRow>
                                <GridRow Class="grid-table">
                                    <GridCol Span="6">
                                        @if (item.ResponseHeaderModifier.Set is { Count: > 0 })
                                        {
                                            @foreach (var set in item.ResponseHeaderModifier.Set)
                                            {
                                                <ColorfulTag>@($"{set.Name}={set.Value}")</ColorfulTag>
                                            }
                                        }
                                    </GridCol>
                                    <GridCol Span="9">
                                        @if (item.ResponseHeaderModifier.Add is { Count: > 0 })
                                        {
                                            @foreach (var add in item.ResponseHeaderModifier.Add)
                                            {
                                                <ColorfulTag>@($"{add.Name}={add.Value}")</ColorfulTag>
                                            }
                                        }
                                    </GridCol>
                                    <GridCol Span="9">
                                        @if (item.ResponseHeaderModifier.Remove is { Count: > 0 })
                                        {
                                            @foreach (var remove in item.ResponseHeaderModifier.Remove)
                                            {
                                                <ColorfulTag>@remove</ColorfulTag>
                                            }
                                        }
                                    </GridCol>
                                </GridRow>
                            </PropertySimpleView>
                        </HTable>
                    }
                </PropertySimpleView>
                <PropertySimpleView Title="@L["SessionPersistence"]" Item="@rule.SessionPersistence"
                                    ExplainField="grpcRoute.spec.rules.sessionPersistence" ShowInJson>
                </PropertySimpleView>

            </HTable>
        }
    }

    @if (GrpcRoute.Status is not null)
    {
        <Divider Orientation="left" Style="font-weight:bold">@L["Status"]</Divider>
        <HTable Bordered Column="1">
            <RouteParentStatusView Parents="@GrpcRoute.Status.Parents"
                                   ExplainFieldPrefix="grpcRoute"
                                   Ns="@GrpcRoute.Metadata.Namespace()">
            </RouteParentStatusView>
        </HTable>
    }


    @if (_svcList is { Count: > 0 })
    {
        <MiniServiceListView Services="@_svcList"></MiniServiceListView>
    }

    <MiniEventListView Uid="@GrpcRoute.Metadata.Uid"></MiniEventListView>
}
